// pages/home/home.js

const app = getApp();
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */

  data: {
    memberList: [
      {
        cont: "重庆逍遥游",
        short: "赏数千载巴渝文化",
        iamges: "../images/hotpot.png",
        hidden: true,
        id: "0",
        invalidActivty: [
          {
            price: "588",
            discount: "6.8折",
            travelInfo: '嘉陵江邮轮两日游',
            image:'/order/jialingjiang.jpg'
          },
          {
            price: "488",
            discount: "5.9折",
            travelInfo: '山城民宿三日游',
            image:'/order/shanchengmingsu.jpg'
          },
          {
            price: "788",
            discount: "6.9折",
            travelInfo: '重庆武隆3天2晚纯玩',
            image:'/order/wulong.webp'
          },
        ]
        //   invalidActivty:[1,2,3]
      },
      {
        cont: "成都逍遥游",
        short: "一起去看大熊猫",
        iamges: "../images/plane.png",
        hidden: true,
        id: "1",
        invalidActivty: [
          {
            price: "548",
            discount: "7.8折",
            travelInfo: '成都到九寨沟两日游',
            image:'/order/jiuzhaigou.webp'
          },
          {
            price: "1969",
            discount: "8.8折",
            travelInfo: '成都自由行4天3晚',
            image:'/order/chengdu.jpeg'
          },
        ]
      },
      {
        cont: "西藏逍遥游",
        short: "领略布达拉宫魅力",
        iamges: "../images/xizang.png",
        hidden: true,
        id: "2",
        invalidActivty: [
          {
            price: "1380",
            discount: "7.8折",
            travelInfo: '西藏拉萨旅游7天6晚',
            image:'/order/lasa.webp'
          },
          {
            price: "1999",
            discount: "6.8折",
            travelInfo: '西藏林芝5日8人小团',
            image:'/order/xizanglingzhi.webp'
          },
        ]
      },
      {
        cont: "海南逍遥游",
        short: "清凉一夏潜水玩乐",
        iamges: "../images/beach.png",
        hidden: true,
        id: "3",
        invalidActivty: [
          {
            price: "818",
            discount: "5.8折",
            travelInfo: '三亚纯玩跟团5天4晚',
            image:'/order/sanya.webp'
          },
          {
            price: "133",
            discount: "6.8折",
            travelInfo: '精品蜈支洲岛一日游',
            image:'/order/wuzhizhou.webp'
          },
        ]
      },
    ],
    value: ''
  },
  isOpen: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.index;
    //   console.log(idx);
    var memberList = that.data.memberList;
    //   console.log(memberList);
    for (let i = 0; i < memberList.length; i++) {
      var opposite = !memberList[i].hidden;
      if (idx == i) {
        memberList[i].hidden = opposite;
      } else {
        memberList[i].hidden = true;
      }
    }
    this.setData({ memberList: memberList });
  },

  buy: function (e) {
    // console.log(e)
    var inner = e.currentTarget.dataset;
    var date = util.formatTime(new Date());
    //console.log(inner.inner)
    app.globalData.myorderlist = inner.inner;
    var userid = wx.getStorageSync('userid')
    //console.log(app.globalData.myorderlist)
    this.setData({
      date: date
    });
    // console.log(date)
    app.globalData.ordertime = date;
    wx.showModal({
      title:'提示',
      content:'是否确认订购？',
      success:function(res){
        if (res.confirm) {
          wx.showLoading({
            title: '正在下单',
          });
          wx.request({
            url: 'http://localhost:8180/webapps/AddOrderServlet',
            data:{'orderName': inner.inner.travelInfo,
                  'orderPrice': inner.inner.price,
                  'scenicImage': inner.inner.image,
                  'userId':userid},
            method:'POST',
            timeout:6000,
            success:function(res){
              wx.hideLoading({
                success: (res) => {},
              })
              if(res.data.data == true){
                wx.showToast({
                  title: '订购成功',
                  icon:'success'
                })
                setTimeout(function(){
                    wx.navigateTo({
                      url: '../order/order',
                    })
                },1500)
              }else{
                wx.showToast({
                  title: '订购失败',
                  icon:'error'
                })
              }
              // console.log(res)
            },
            fail:function(){
              wx.hideLoading({
                success: (res) => {},
              })
              wx.showToast({
                title: '连接服务器失败',
                icon:'error'
              })
            }
          })
        }else if (res.cancel) {
          wx.showToast({
            title: '已取消订购',
            icon:'none'
          })
        } else {
          wx.showToast({
            title: '异常请求',
            icon: 'error'
          })
        }
      }
    })
  },



  getSearchResult: function () {
    var value = this.data.value
    if (value == '') {
      wx.showToast({
        title: '请输入关键字',
        icon:'error'
      })
    } else {
        wx.navigateTo({
          url: '../search/search?value=' + value
        })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.setData({
      value:''
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})